<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="总成本" prop="sCost">
        <el-input
          v-model="queryParams.sCost"
          placeholder="请输入总成本"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总收入" prop="sIncome">
        <el-input
          v-model="queryParams.sIncome"
          placeholder="请输入总收入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支出类型" prop="expensesType">
        <el-input
          v-model="queryParams.expensesType"
          placeholder="请输入支出类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收入类型" prop="incomeType">
        <el-input
          v-model="queryParams.incomeType"
          placeholder="请输入收入类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['financial:statistics:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['financial:statistics:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['financial:statistics:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['financial:statistics:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="statisticsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="总成本（单位：元）" align="center" prop="sCost" width="150"/>
      <el-table-column label="总收入（单位：元）" align="center" prop="sIncome" width="150"/>
      <el-table-column label="利润（单位：元）" align="center" prop="sProfit" width="150"/>
      <el-table-column label="支出明细" align="center" prop="expensesDetail" />
      <el-table-column label="收入明细" align="center" prop="incomeDetail" />
      <el-table-column label="财务报表文件" align="center" prop="sFiles" />
      <el-table-column label="支出类型" align="center" prop="expensesType" />
      <el-table-column label="收入类型" align="center" prop="incomeType" />
      <el-table-column label="备注" align="center" prop="notes" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['financial:statistics:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['financial:statistics:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改财务统计对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="总成本（单位：元）" prop="sCost">
          <el-input v-model="form.sCost" placeholder="请输入总成本" />
        </el-form-item>
        <el-form-item label="总收入（单位：元）" prop="sIncome">
          <el-input v-model="form.sIncome" placeholder="请输入总收入" />
        </el-form-item>
        <el-form-item label="利润（单位：元）" prop="sProfit">
          <el-input v-model="form.sProfit" placeholder="请输入利润" />
        </el-form-item>
        <el-form-item label="支出明细" prop="expensesDetail">
          <el-input v-model="form.expensesDetail" placeholder="请输入支出明细" />
        </el-form-item>
        <el-form-item label="收入明细" prop="incomeDetail">
          <el-input v-model="form.incomeDetail" placeholder="请输入收入明细" />
        </el-form-item>
        <el-form-item label="财务报表文件" prop="sFiles">
          <file-upload v-model="form.sFiles"/>
        </el-form-item>
        <el-form-item label="支出类型" prop="expensesType">
          <el-input v-model="form.expensesType" placeholder="请输入支出类型" />
        </el-form-item>
        <el-form-item label="收入类型" prop="incomeType">
          <el-input v-model="form.incomeType" placeholder="请输入收入类型" />
        </el-form-item>
        <el-form-item label="备注" prop="notes">
          <el-input v-model="form.notes" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStatistics, getStatistics, delStatistics, addStatistics, updateStatistics } from "@/api/financial/statistics";

export default {
  name: "Statistics",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 财务统计表格数据
      statisticsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sCost: null,
        sIncome: null,
        sProfit: null,
        expensesDetail: null,
        incomeDetail: null,
        sFiles: null,
        expensesType: null,
        incomeType: null,
        notes: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sCost: [
          { required: true, message: "总成本不能为空", trigger: "blur" }
        ],
        sIncome: [
          { required: true, message: "总支出不能为空", trigger: "blur" }
        ],
        sProfit: [
          { required: true, message: "利润不能为空", trigger: "blur" }
        ],
        expensesDetail: [
          { required: true, message: "支出明细不能为空", trigger: "blur" }
        ],
        incomeDetail: [
          { required: true, message: "收入明细不能为空", trigger: "blur" }
        ],
        sFiles: [
          { required: true, message: "财务报表文件不能为空", trigger: "blur" }
        ],
        expensesType: [
          { required: true, message: "支出类型不能为空", trigger: "blur" }
        ],
        incomeType: [
          { required: true, message: "收入类型不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询财务统计列表 */
    getList() {
      this.loading = true;
      listStatistics(this.queryParams).then(response => {
        this.statisticsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        sId: null,
        sCost: null,
        sIncome: null,
        sProfit: null,
        expensesDetail: null,
        incomeDetail: null,
        sFiles: null,
        expensesType: null,
        incomeType: null,
        notes: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.sId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加财务统计";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const sId = row.sId || this.ids
      getStatistics(sId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改财务统计";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.sId != null) {
            updateStatistics(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStatistics(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const sIds = row.sId || this.ids;
      this.$modal.confirm('是否确认删除财务统计编号为"' + sIds + '"的数据项？').then(function() {
        return delStatistics(sIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('financial/statistics/export', {
        ...this.queryParams
      }, `statistics_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
